<template>
  <div class="student-edit-container">
    <!-- 返回箭头 -->
    <div class="header">
      <el-button 
        icon="arrow-left"
        @click="handleBack"
        class="back-button"
      ></el-button>
    </div>
    
    <!-- 表单区域 -->
    <el-form 
      :model="form"
      ref="formRef"
      label-width="120px"
      class="student-form"
      :rules="rules"
    >
      <el-form-item v-if="isEditMode" label="学号" prop="id">
        <el-input v-model="form.id" :disabled="isEditMode" placeholder="请输入学号" />
      </el-form-item>
      
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name" placeholder="请输入姓名" />
      </el-form-item>
      
      <el-form-item label="照片">
        <el-upload
          action="#"
          list-type="picture-card"
          :auto-upload="false"
          :on-change="handlePhotoChange"
          :file-list="form.photo ? [{ url: form.photo }] : []"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
        <div class="el-upload__tip">点击上传照片（非必填）</div>
      </el-form-item>

      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="form.sex">
          <el-radio label="男" :value="1">男</el-radio>
          <el-radio label="女" :value="0">女</el-radio>
        </el-radio-group>
      </el-form-item>
      
      <!-- <el-form-item label="年龄" prop="age">
        <el-input v-model.number="form.age" type="number" placeholder="请输入年龄" />
      </el-form-item> -->
      
      <el-form-item label="年级" prop="grade">
        <el-select v-model="form.grade" placeholder="请选择年级">
          <el-option label="初一" :value="1" />
          <el-option label="初二" :value="2" />
          <el-option label="初三" :value="3" />
         
        </el-select>
      </el-form-item>
      

      
      <!-- <el-form-item label="课程">
        <el-select v-model="form.course" placeholder="请选择课程">
          <el-option label="高等数学" value="高等数学" />
          <el-option label="线性代数" value="线性代数" />
          <el-option label="计算机基础" value="计算机基础" />
          <el-option label="程序设计" value="程序设计" />
          <el-option label="数据结构" value="数据结构" />
        </el-select>
      </el-form-item> -->
      
      <el-form-item label="联系方式">
        <el-input v-model="form.contactWay" placeholder="请输入联系方式" />
      </el-form-item>

     
    </el-form>
    
    <!-- 底部按钮 -->
    <div class="button-group">
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import * as studentApi from '@/api/studentManage'

const route = useRoute()
const router = useRouter()
const formRef = ref(null)
const isEditMode = ref(false)

// 表单验证规则
const rules = {
  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }]
}

// 表单数据
const form = reactive({
  id: '',
  name: '',
  studentImg: '',
  channel: 2,
  sex: 1,
  grade: 1,
  contactWay: '',
})

// 模拟获取学生数据
const getStudentData = () => {
  const studentId = route.params.id
  // 判断是否是编辑模式
  isEditMode.value = studentId !== 'new'

  if (isEditMode.value) {
    studentApi.getStudentById(studentId).then(res => {
      if (res.code === 200) {
        Object.assign(form, res.data)
      } else {
        ElMessage.error(res.msg || '获取学生数据失败')
        router.push('/studentManage/index')
      }
    })

    
  } else {
    form.id = ''
  }
}

// 返回上一页
const handleBack = () => {
  router.go(-1)
}

// 取消按钮
const handleCancel = () => {
  router.go(-1)
}

// 提交表单
const handleSubmit = async () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      const res = await studentApi.addOrUpdateStudent(form)
      if (res.code === 200) {
        ElMessage.success('操作成功')
        router.push('/studentManage/index')
      } else {
        ElMessage.error(res.msg || '操作失败')
      }
    }
  })
}

// 页面加载时获取数据
onMounted(() => {
  getStudentData()
})

// 处理照片上传
const handlePhotoChange = (file) => {
  // 在实际项目中，这里应该上传文件到服务器并获取URL
  // 这里使用临时URL模拟
  form.photo = URL.createObjectURL(file.raw)
}
</script>

<style scoped>
.student-edit-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.header {
  margin-bottom: 30px;
}

.back-button {
  font-size: 18px;
}

.student-form {
  margin-bottom: 40px;
}

.button-group {
  display: flex;
  justify-content: center;
  gap: 20px;
}
</style>